from pav4_lexer import lex
from pavdef import*
outfile = open("ex.txt.py", "w")
file = open("ex.txt", "r")
outfile.write("from pavdef import* \n")
lines = file.readlines()
file.close()

arr = list()

function_name = ["ucase", "print", "lcase","int","float","lineinput","left"]
loops = ["while", "wend","iftrue","ifequal","fig","else"]
functions = {"ucase":0,"print":-1,"lcase":0,"int":-1,"left":-2}

def find_param(flist,nparam):
    arr = list()
    for i in range (nparam):
        arr.append(flist[i])
    return arr


def create_assign_var(var_name,assign_list): ## creates variable assignment part
    after_equal = ""
    for i in range (len(assign_list)):
        after_equal += assign_list[i]
    output = var_name + " = " + after_equal
    return output

def create_func_var(var_name , func_list,indent): ## used when functions are used
    output = "\n"
    forward = 0
    #print ("hello",func_list)
    for i in range (len(func_list)):
        if func_list[i] in function_name:
            param_no = functions[func_list[i]]
            if param_no >= 0 : # subcall
                params = find_param(func_list[i + 1:], param_no)
                output += handle_function_sub(var_name,func_list[i],params) + "\n"
            if param_no < 0: #functioncall
                params = find_param(func_list[i + 1:], abs(param_no)-1)
                output += handle_function_fc(var_name, func_list[i], params) + "\n"

    output = output.replace("\n", "\n" + chr(32) * indent)
    return output



def stringindent(x, lev):  # for indentation
    return chr(32) * lev + x + "\r\n"

def check_indent(list):
    output = ""
    if list[0] == "loop":
        output = True
    else :
        output = False

    return output

indent = 0

def parser(list):
    global indent
    type_list = []  # contains type of data
    name_list = []  # value of data
    output = ""
    for i in range(0, len(list)):
        type_list.append(list[i][0])
        name_list.append(list[i][1])

    if type_list[0] == "variable":
        assign_list = []
        func_list = []
        var_name = name_list[0]
        for i in range (1,len(name_list)):
            if type_list[i] != "function":
                assign_list.append(name_list[i]) ## stores assignment part
                break
        #print(assign_list)
        func_list = name_list[len(assign_list)+1:]




        if len(assign_list) > 0 and len(func_list) == 0: ## when no functions are used
            final_var = create_assign_var(var_name, assign_list)
            output = final_var


        if len(assign_list) == 0 and len(func_list) > 0: ## when theres no assignment and functions are used
            output = create_func_var(var_name,func_list,indent)


        if len(assign_list) > 0 and len(func_list) > 0: ## when both assignment is done and function(s) is used
            final_var = create_assign_var(var_name, assign_list)
            func_part = create_func_var(var_name,func_list,indent)
            output = final_var + "\n" + func_part
            #print(assign_list)



        if len(assign_list) == 0 and len(func_list)==0: ## when theres no assignment and function call
            return (var_name+ " = 0 ")

    if type_list[0] == "loop" :
        output = ""
        if name_list[0]== "while":
            if len(name_list)>1 :
                print("error in syntax")
            else:
                output = "while True :"
                indent += 4
        if name_list[0]== "wend":
            indent-=4
        if name_list[0] == "fig":
            indent -= 4
        if name_list[0] == "iftrue" :
            rest_list = name_list[1:]
            if len(rest_list) != 1 :
                print("syntax error")
            else :
                output = "if " + rest_list[0] +" == True :"
                indent += 4
        if name_list[0] == "ifequal":
            rest_list = name_list[1:]
            if len(rest_list) != 2:
                print("syntax error")
            else:
                output = "if " + rest_list[0] + " == " + rest_list[1] + " :"
                indent += 4
        if name_list[0] == "else":
            if len(name_list)!=1 :
                print("syntax error")
            else :
                output = "else:"
                indent += 4

    if check_indent(type_list) == False:
        return stringindent(output, indent)
    elif check_indent(type_list) == True:
        return stringindent(output, indent - 4)



for line in lines:
    line = line.strip()
    if line == "":
        pass
    else :
        arr.append(line)


for i in range (0,len(arr)):
    output = ( parser((lex(arr[i])))  )+"\n"
    outfile.write(output)

#output = create_func_var("var",["print","lcase","ucase"])
#print(output)
